home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / mkmanifest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-08  |  2.0 KB  |  102 lines

  1. /*
  2.  * A program to create a manifest (shipping list) that is a shell script
  3.  * to return a Unix file name to it's original state after it has been
  4.  * clobbered by MSDOS's file name restrictions.
  5.  *
  6.  *    This code also used in arc, mtools, and pcomm
  7.  */
  8.  
  9. #include "sysincludes.h"
  10. #include "msdos.h"
  11. #include "patchlevel.h"
  12.  
  13. static char *dos_name2();
  14.  
  15. void
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     int i;
  21.     char *name, *new_name;
  22.  
  23.     /* print the version */
  24.     if(argc >= 2 && strcmp(argv[1], "-V") == 0) {
  25.         printf("Mtools version %s, dated %s\n", VERSION, DATE);
  26.         exit(0);
  27.     }
  28.  
  29.     if (argc == 1) {
  30.         fprintf(stderr, "Usage: mkmanifest [-V] <list-of-files>\n");
  31.         exit(1);
  32.     }
  33.  
  34.     for (i=1; i<argc; i++) {
  35.                     /* zap the leading path */
  36.         if ((name = strrchr(argv[i], '/')))
  37.             name++;
  38.         else
  39.             name = argv[i];
  40.                     /* create new name */
  41.         new_name = dos_name2(name);
  42.  
  43.         if (strcasecmp(new_name, name))
  44.             printf("mv %s %s\n", new_name, name);
  45.     }
  46.     exit(0);
  47. }
  48.  
  49. static char *
  50. dos_name2(name)
  51. char *name;
  52. {
  53.     static char *dev[9] = {"con", "aux", "com1", "com2", "lpt1", "prn",
  54.     "lpt2", "lpt3", "nul"};
  55.     char *s, *temp, *ext;
  56.     char buf[MAX_PATH];
  57.     int i, dot;
  58.     static char ans[13];
  59.  
  60.     strcpy(buf, name);
  61.     temp = buf;
  62.                     /* separate the name from extension */
  63.     ext = "";
  64.     dot = 0;
  65.     for (i=strlen(buf)-1; i>=0; i--) {
  66.         if (buf[i] == '.' && !dot) {
  67.             dot = 1;
  68.             buf[i] = '\0';
  69.             ext = &buf[i+1];
  70.         }
  71.         if (isupper(buf[i]))
  72.             buf[i] = tolower(buf[i]);
  73.     }
  74.                     /* if no name */
  75.     if (*temp == '\0')
  76.         temp = "x";
  77.                     /* if name is a device */
  78.     for (i=0; i<9; i++) {
  79.         if (!strcasecmp(temp, dev[i])) 
  80.             *temp = 'x';
  81.     }
  82.                     /* name too long? */
  83.     if (strlen(temp) > 8)
  84.         *(temp+8) = '\0';
  85.                     /* extension too long? */
  86.     if (strlen(ext) > 3)
  87.         *(ext+3) = '\0';
  88.                     /* illegal characters? */
  89.     while ((s = strpbrk(temp, "^+=/[]:',?*\\<>|\". ")))
  90.         *s = 'x';
  91.  
  92.     while ((s = strpbrk(ext, "^+=/[]:',?*\\<>|\". ")))
  93.         *s = 'x';
  94.  
  95.     strcpy(ans, temp);
  96.     if (*ext) {
  97.         strcat(ans, ".");
  98.         strcat(ans, ext);
  99.     }
  100.     return(ans);
  101. }
  102.